<?php
/**
 * Created by PhpStorm.
 * User: 我型我素
 * Date: 2018/2/26
 * Time: 14:59
 */

namespace SDXApi\Controller;


use SDXApi\Common\Tools\AppVersion;

class GreetingCardController extends AppController
{
    //模板设置
    protected $template_info = array(
        '1'=>array('background'=>'b1'),
        '2'=>array('background'=>'b2'),
        '3'=>array('background'=>'b3'),
    );

    //贺卡内容设置
    protected $template_content = array(
        array('text'=>'希望不管再过多少年，不管你是开心还是难过，欣喜还是委屈，都能记得我。因为我始终都站在，你一回头就能看得见我的地方。'),
        array('text'=>'无论多久没见，见面时依然熟稔得像是昨天才刚一起逛过街吃过饭一样。分别时也从不遗憾，因为我们知道，只要一个电话一条微信，就还能在老地方看到对方。这就是闺蜜，这就是我和你。'),
        array('text'=>'他们说，闺蜜就像是星星，你不一定会经常见到她，但是你知道，她会一直在那里。在最黑暗的时候，会陪着你一起等天亮。我们都是彼此的星星。')
    );

    //分享内容
    protected $share_data = array(
        'time_line'=>array(
            'title'=>'亲爱的，我有些悄悄话想对你说',
            'pic'=>'https://oss2.shidexian.com/spe/share_small.jpg',
            'link'=>'https://w.shidexian.com/?index=/shareSuc'
        ),
        'app_messages'=>array(
            'title'=>'亲爱的，我有些悄悄话想对你说',
            'desc'=>'这份礼物送给你，谢谢你总在我的身边。',
            'pic'=>'https://oss2.shidexian.com/spe/share_small.jpg',
            'link'=>'https://w.shidexian.com/?index=/shareSuc'
        )
    );

    //商品
    protected $goods = array(
        'goods_id'=>'18990',
        'goods_name'=>'茶咕噜特调花茶四连包12g'
    );

    //红包
    protected $bonus = array(
        'bonus_id'=>'985',
        'bonus_name'=>'女神节10元运费减免红包'
    );

    //专题id
    protected $seminar_id = "1540";

    public function _initialize($apptoken, $apptime)
    {
        parent::_initialize($apptoken, $apptime); // TODO: Change the autogenerated stub
    }

    public function get_greeting_card()
    {
        $uid = I('post.uid');
        $data = array();
        $data['template_content'] = $this->get_template_content();
        $data['is_new_user'] = $this->check_is_new_user($uid);

        returnApiSuccess($data);
    }

    public function create_card()
    {
        $uid = I('post.uid');
        $name = I('post.name');
        $title = I('post.title');
        $content = I('post.content');
        $template_id = I('post.template_id');

        if(empty($uid) || empty($name) || empty($content)){
            returnApiError('缺少参数');
        }

        $data = array();
        $data['uid'] = $uid;
        $data['name'] = $name;
        $data['title'] = $title;
        $data['content'] = $content;
        $data['template_id'] = $template_id;
        $data['create_time'] = time();

        $card_id = M('greeting_card')->add($data);

        if(!empty($card_id)){
            $data['card_id'] = $card_id;
            returnApiSuccess($data,'生成祝福成功');
        }else{
            returnApiError('生成祝福失败，请重试');
        }
    }

    public function show()
    {
        $uid = I('post.uid');
        $card_id = I('post.card_id');

        if(empty($card_id) || empty($uid)){
            returnApiError('参数错误');
        }

        $map = array('card_id'=>$card_id);
        $data = M('greeting_card')->where($map)->find();

        if(!empty($data)){
            $data['is_new_user'] = $this->check_is_new_user($uid);
            $data['goods'] = $this->get_goods();
            $data['bonus'] = $this->get_bonus();
            $data['seminar_id'] = $this->seminar_id;
            returnApiSuccess($data);
        }else{
            returnApiError('没有数据');
        }
    }

    public function get_share_data(){
        $uid = I('post.uid');

        $data = array();
        $data['is_new_user'] = $this->check_is_new_user($uid);
        $data['share_data'] = $this->get_share_content();
        $data['goods'] = $this->get_goods();
        $data['bonus'] = $this->get_bonus();
        $data['seminar_id'] = $this->seminar_id;

        if(AppVersion::checkAppName('ios')){
            $data['share_data'] = $this->changIosShareData($data['share_data']);
        }

        returnApiSuccess($data);
    }

    public function changIosShareData($shareData){
        $shareData['time_line']['link'] = "https://w.shidexian.com/";
        $shareData['app_messages']['link'] = "https://w.shidexian.com/";
        return $shareData;
    }

    public function get_share_content()
    {
        return $this->share_data;
    }

    private function get_template_info($template_id)
    {
        return $this->template_info[$template_id];
    }

    private function get_template_content($template_id)
    {
        if(empty($template_id)){
            return $this->template_content;
        }else{
            return $this->template_content[$template_id];
        }
    }

    private function check_is_new_user($uid)
    {
        $flag = 1;
        $order_map = array('user_id'=>$uid,'order_status'=>array('in',array(OS_CONFIRMED,OS_SPLITED)),'shipping_status'=>SS_RECEIVED);
        $order_count = M('order_info')->where($order_map)->count();
        if($order_count>C('NEW_USER_ORDER_NUMBER')){
            $flag = 0;
        }

        return $flag;
    }

    public function get_goods(){
        return $this->goods;
    }

    public function get_bonus(){
        return $this->bonus;
    }
}